package com.example.culturapp;

import java.io.ByteArrayOutputStream;
//import java.util.ArrayList;

//import com.google.gson.Gson;
//import com.google.gson.GsonBuilder;



import android.app.ListFragment;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;

/**
 * A placeholder fragment containing a simple view.
 */
public class PlaceholderFragment extends ListFragment {
	protected ListAdapter listAdapter;
	public int x;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		//listAdapter= new ItemAdapterHoy(getActivity(), );+
		if(savedInstanceState!=null){
			//String jsonMyObject;
			//jsonMyObject = savedInstanceState.getString("myObject");
			//Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
			x=savedInstanceState.getInt("x");
			getActivity().getApplicationContext();
			CulturAppApplication app=(CulturAppApplication)getActivity().getApplicationContext();//CulturAppApplication.getInstance();//(CulturAppApplication) getActivity().getApplication();
			if(x==0){
				listAdapter= app.listAdapterToday;
			}
			if(x==1){
				listAdapter= app.listAdapterWeek;
			}
			//listAdapter= new ItemAdapterHoy(getActivity(), gson.fromJson(jsonMyObject, ArrayList.class));
			//View result = super.onCreateView(inflater, container, savedInstanceState);
			//result.get .setAdapter(listAdapter);
			//return result;
		}
		return super.onCreateView(inflater, container, savedInstanceState);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		this.getListView().setAdapter(listAdapter);
		this.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapter, View view,
					int position, long arg) {
				ItemHoy item = (ItemHoy) listAdapter.getItem(position);
				
				//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				if(view.getContext()!=null){
					Intent intent = new Intent(view.getContext(), MostrarEventoActivity.class);
					intent.putExtra("nombre",item.getNombre());
					intent.putExtra("lugar",item.getLugar());
					intent.putExtra("fecha",item.getFecha());
					intent.putExtra("descripcion",item.getDescripcion());
					intent.putExtra("hora",item.getHora());
					Bitmap bitmap;
			        int img=0;
			       	try{
			       		bitmap = (Bitmap) item.getImagen();
			   		}catch(Exception e){
			   			bitmap= null;
			   			img = (Integer) item.getImagen();
			   		}
			       	if(bitmap==null){
			       		intent.putExtra("imagen",img);
			       		intent.putExtra("isBitmap", false);
			       	}else{
			       		ByteArrayOutputStream stream = new ByteArrayOutputStream();
			       		bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
			       		byte[] byteArray = stream.toByteArray();
			       		intent.putExtra("bitmap", byteArray);
			       		intent.putExtra("isBitmap", true);
			       	}
					view.getContext().startActivity(intent);
				}else{
					Log.d(null, "***************************NULL");
				}
			};

		});
	};
			
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		//ItemAdapterHoy adapter= (ItemAdapterHoy) listAdapter;
		//outState.putParcelableArrayList("listAdapter",(ArrayList<ItemHoy>) adapter.getItems());
		//outState.putParcelable("context",adapter.getContext());
		//ItemAdapterHoy adapter= (ItemAdapterHoy) listAdapter;
		//ArrayList<ItemHoy> list = adapter.getItems();
		//CulturAppApplication app=(CulturAppApplication) getActivity().getApplication();
		//app.
		/*Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
		outState.putString("myObject", gson.toJson(list));*/
		outState.putInt("x", x);
	}
//	
//	@Override
//	public void onCreate(Bundle savedInstanceState) {
//		super.onCreate(savedInstanceState);
//		if(savedInstanceState!=null){
//			ArrayList<ItemHoy> l= (ArrayList<ItemHoy>) savedInstanceState.getParcelableArrayList("listAdapter");
//			listAdapter = new ItemAdapterHoy(getView().getContext(),l);
//		}		
//	}
}
